/**
 * 
 */
package com.disys.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

import com.disys.facade.RecReferenceValueFacade;
import com.disys.model.RecReferenceValue;

/**
 * @author Ubaid Raja
 * 
 */
@ViewScoped
@ManagedBean(name="referenceMB")
public class RecReferenceValueMB extends AbstractMB implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8451705072287228786L;
	private String recordType;
	private Map<String, String> supportValues;
	private List<RecReferenceValue> list;

	private RecReferenceValueFacade objFacade;
	//private String[] referenceValues;


	public RecReferenceValueMB() {
		HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		String type = req.getParameter("type");
		//String type = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("type");
		System.out.println("Type ==>>>> "+type);
		recordType = type;
		System.out.println("RecReferenceValueMB");
		//supportValues = new HashMap<String, String>();
		objFacade = new RecReferenceValueFacade();
		list = objFacade.findSupportingValues("RecReferenceValue", this.getRecordType());
		//supportValues = getListOfSupportValues(list);
		//referenceValues = new String[list.size()];
		//list = new ArrayList<RecReferenceValue>();
	}


	/**
	 * @return the listOfSupportValues
	 */
	public Map<String, String> getListOfSupportValues(List<RecReferenceValue> list) {
//		List<RecReferenceValue> list = new ArrayList<RecReferenceValue>();
//		list = objFacade.findSupportingValues("RecReferenceValue",
//				this.getRecordType());
		if (supportValues != null || supportValues.size() > 0) {
			supportValues.clear();
		}
		String key = "";
		String value = "";
		for (RecReferenceValue obj : list) {
			key = obj.getColumnName();
			value = obj.getColumnValue();
			supportValues.put(key, value);
		}
		return supportValues;
	}

	
	public String update(ActionEvent event){
		System.out.println("update called");
//		Iterator<Entry<String, String>> it = supportValues.entrySet().iterator();
//	    while (it.hasNext()) {
//	        Map.Entry pairs = (Map.Entry)it.next();
//	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
//	        it.remove(); // avoids a ConcurrentModificationException
//	    }
		String object = "RecReferenceValue";
		String cName =  "";
		String cValue = "";
		for(int i=0; i<list.size();i++){
			cName = list.get(i).getColumnName();
			cValue = list.get(i).getColumnValue();
			objFacade.updateReferences(object, cName, cValue, recordType);
		}
		displayInfoMessageToUser("Record updated successfully!!!");
		return "";
	}
	
	public String cancel(ActionEvent event){
		System.out.println("cancel called");
		displayInfoMessageToUser("Changes discarded!!!");
		return "";
	}
	
	/**
	 * @return the recordType
	 */
	public String getRecordType() {
		return recordType;
	}

	/**
	 * @param recordType
	 *            the recordType to set
	 */
	public void setRecordType(String recordType) {
		this.recordType = recordType;
	}
	
	
	/**
	 * @return the list
	 */
	public List<RecReferenceValue> getList() {
//		if(list != null){
//			System.out.println("List get "+list);
//		}
		if(list == null){
			list = new ArrayList<RecReferenceValue>();
		}
		list = objFacade.findSupportingValues("RecReferenceValue", this.getRecordType());
		return list;
	}

	/**
	 * @param list
	 *            the list to set
	 */
	public void setList(List<RecReferenceValue> list) {
		if(list != null){
			System.out.println("List set");
		}
		this.list = list;
	}


	/**
	 * @return the supportValues
	 */
	public Map<String, String> getSupportValues() {
		return supportValues;
	}


	/**
	 * @param supportValues the supportValues to set
	 */
	public void setSupportValues(Map<String, String> supportValues) {
		this.supportValues = supportValues;
	}


	/**
	 * @param referenceValues the referenceValues to set
	 */
//	public void setReferenceValues(String[] referenceValues) {
//		this.referenceValues = referenceValues;
//	}
	
}
